<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

use Think\Controller;


/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class PublicController extends Controller
{
    //认证原始信息
    public function index()
    {
        if(IS_POST){
            $con['realname'] = I('realname');
            $con['dksign'] = I('dksign');
            $user = M('core_data')->where($con)->find();
            if($user){
                $this->redirect('register',array('realname'=>I('realname'),'dksign'=>I('dksign'),));
            }else{
                $this->error('用户不存在');
            }
        }else{
            $this->display();
        }
    }
    //注册
    public function register()
    {   
        D('member')->startTrans();
        D('ucenter_member')->startTrans();
        D('core_user_info')->startTrans();
        if(IS_POST){
            switch (I('go')) {
                case '注册':
                    $data['username'] = I('dksign');
                    $data['password'] = md5(substr(I('dksign'),-6));
                    $result = D('ucenter_member')->add($data);
                    $result2 = D('member')->add(array('nickname'=>I('realname'),'uid'=>$result,'status'=>1));
                    //注册信息关联表
                    $tmp = M('core_data')->where('dksign = "'.I('dksign').'"')->field('unit,depart,group')->find();
                    $con['title'] = $tmp['depart'];
                    $con['unit'] = $tmp['unit'];
                    $class = M('core_class')->where($con)->find();
                    if(!$class){
                        //如果所属班不存在，自动生成班级
                        $result3 = M('core_class')->add($con);
                    }else{
                        $result3 = $class['id'];
                    }
                    //把所有信息保存到信息关联表里user_info
                    $aaa['class_id'] = $result3;
                    $aaa['unit_id'] = $tmp['unit'];
                    $aaa['uid'] = $result;
                    $aaa['group_id'] = $tmp['group'];
                    $r = D('core_user_info')->add($aaa);
                    
                    if($result&&$result2&&$r){
                        D('member')->commit();
                        D('ucenter_member')->commit();
                        D('core_user_info')->commit();
                        $_SESSION['oc_home']['uid'] = $result['id'];
                        $this->success('注册成功',U('setInfo'));
                    }else{
                        D('member')->rollback();
                        D('ucenter_member')->rollback();
                        D('core_user_info')->rollback();
                        $this->error('注册失败');
                    }
                    break;
                case '模拟注册':
                    $con['username'] = I('dksign');
                    $con['password'] = md5(I('dksign'));
                    $result = M('ucenter_member')->where($con)->find();
                    if($result){
                        $_SESSION['oc_home']['uid'] = $result['id'];
                        $this->success('注册成功',U('setInfo'));
                    }else{
                        $this->error('注册失败');
                    }
                    break;
            }
        }else{
            $this->assign('realname',I('realname'));
            $this->assign('dksign',I('dksign'));
            $this->display();
        }
    }
    //登录
    public function login()
    {
        if(IS_POST){
            $con['username|mobile'] = I('username');
            $con['password'] = md5(I('password'));
            $result = M('core_userinfo_view')->where($con)->find();
            // dump($result);exit;
            if($result){
                $_SESSION['oc_home']['uid'] = $result['uid'];
                $_SESSION['oc_home']['username'] = $result['username'];
                $_SESSION['oc_home']['group'] = $result['group_id'];
                $this->success('登录成功',U('person'));
            }else{
                $this->error('登录失败');
            }
        }else{
            $this->assign('realname',I('realname'));
            $this->assign('dksign',I('dksign'));
            $this->display();
        }
    }
    //个人中心
    public function person()
    {
        if(IS_POST){
            $con['username'] = I('dksign');
            $con['password'] = md5(I('dksign'));
            $result = M('ucenter_member')->where($con)->find();
            if($result){
                $_SESSION['oc_home']['uid'] = $result['id'];
                $this->success('登录成功',U('person'));
            }else{
                $this->error('登录失败');
            }
        }else{
            $this->assign('realname',I('realname'));
            $this->assign('dksign',I('dksign'));
            $this->display();
        }
    }

    //完善信息
    public function setInfo()
    {
        if(IS_POST){
            if(!I('sex')) $this->error('请输入性别!');
            if(!I('mobile')) $this->error('请输入手机号!');
            if(!I('province')) $this->error('请输入省!');
            if(!I('city')) $this->error('请输入市!');
            if(!I('district')) $this->error('请输入区!');
            D('member')->create();
            D('ucenter_member')->create();
            D('member')->uid = $_SESSION['oc_home']['uid'];
            D('member')->birthday = strtotime(I('birthday'));
            D('member')->pos_province = I('province');
            D('member')->pos_city = I('city');
            D('member')->pos_district = I('district');
            $result = D('member')->where('uid = '.$_SESSION['oc_home']['uid'])->save();
            $result2 = D('ucenter_member')->where('id = '.$_SESSION['oc_home']['uid'])->save();
            if($result&&$result2){
                $this->success('完善信息成功',U('person'));
            }else{
                $this->error('完善信息失败');
            }
        }else{
            $this->assign('pro',pro());
            $this->display();
        }
    }
    //-----------获取下级----------------@
    function getChild(){
      $prov=$_GET['child'];
      if($_GET['child']){      
          $childModel=M('district');
          $child=$childModel->where('upid = '.$prov)->select();
          echo json_encode($child);
      }
    }
    

}